package com.cyy.demo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.IOException;

/**
 * json文件读取类
 */

public class ToolFileUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(ToolFileUtils.class);


    public static JSONObject getJSONObjectFromFile(String fileName) {
        File file = null;
        JSONObject jsonObject = null;
        try {
            file = ResourceUtils.getFile("classpath:/" + fileName);
            if (!file.exists()) {
                LOGGER.info(fileName + "文件不存在");
                return null;
            }
            LOGGER.info("开始读取+fileName+文件");
            String content = FileUtils.readFileToString(file, "UTF-8");
            jsonObject = JSON.parseObject(content);
            LOGGER.info("读取+fileName+文件完成");
        } catch (IOException e) {
            LOGGER.error("读取+fileName+文件失败-----" + e.getMessage());
        }
        return jsonObject;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = getJSONObjectFromFile("warnedfaces.json");
        System.out.println(jsonObject);
    }

}